<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Linux-bash | Abel&#39;Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="记录一下Linux运维的时候常用的bash的知识 目录相关获取当前文件的绝对目录123456if [ -L $0 ]then    BASE_DIR&#x3D;&#96;dirname $(readlink $0)&#96;else    BASE_DIR&#x3D;&#96;dirname $0&#96;fi  $0 获取当前可执行程序的名称； if [ -L $0 ] 用于检查一个名字是否为link； $(readlink $0) 用于获取一个">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux-bash">
<meta property="og:url" content="http://example.com/2020/11/12/2020/2020-11-12-bash/index.html">
<meta property="og:site_name" content="Abel&#39;Blog">
<meta property="og:description" content="记录一下Linux运维的时候常用的bash的知识 目录相关获取当前文件的绝对目录123456if [ -L $0 ]then    BASE_DIR&#x3D;&#96;dirname $(readlink $0)&#96;else    BASE_DIR&#x3D;&#96;dirname $0&#96;fi  $0 获取当前可执行程序的名称； if [ -L $0 ] 用于检查一个名字是否为link； $(readlink $0) 用于获取一个">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-11-11T16:00:00.000Z">
<meta property="article:modified_time" content="2021-07-28T09:12:27.841Z">
<meta property="article:author" content="Abel Sean">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Abel&#39;Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Abel&#39;Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2020/2020-11-12-bash" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/12/2020/2020-11-12-bash/" class="article-date">
  <time datetime="2020-11-11T16:00:00.000Z" itemprop="datePublished">2020-11-12</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Linux-bash
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>记录一下Linux运维的时候常用的bash的知识</p>
<h1 id="目录相关"><a href="#目录相关" class="headerlink" title="目录相关"></a>目录相关</h1><h2 id="获取当前文件的绝对目录"><a href="#获取当前文件的绝对目录" class="headerlink" title="获取当前文件的绝对目录"></a>获取当前文件的绝对目录</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> [ -L <span class="variable">$0</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    BASE_DIR=`dirname $(readlink <span class="variable">$0</span>)`</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    BASE_DIR=`dirname <span class="variable">$0</span>`</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<p>$0 获取当前可执行程序的名称；</p>
<p>if [ -L $0 ] 用于检查一个名字是否为link；</p>
<p>$(readlink $0) 用于获取一个文件的正式的位置；</p>
<p>dirname 直接获取某个文件所在目录</p>
<h1 id="本地IP地址抓取"><a href="#本地IP地址抓取" class="headerlink" title="本地IP地址抓取"></a>本地IP地址抓取</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LOCALIP=`ip addr|grep <span class="string">&quot;eth0:&quot;</span> -A 3|grep <span class="string">&quot;inet &quot;</span>|sed -r <span class="string">&quot;s@.* (([0-9]&#123;1,3&#125;\.)&#123;3&#125;[0-9]&#123;1,3&#125;)/(32|24|16|8) .*@\1@&quot;</span>`</span><br></pre></td></tr></table></figure>

<p>中间是使用了 s@正则@\1@ 来抓取了其中的IP地址；</p>
<h1 id="if语句"><a href="#if语句" class="headerlink" title="if语句"></a>if语句</h1><table>
<thead>
<tr>
<th align="left">指令</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">–b</td>
<td align="left">当file存在并且是块文件时返回真</td>
</tr>
<tr>
<td align="left">-c</td>
<td align="left">当file存在并且是字符文件时返回真</td>
</tr>
<tr>
<td align="left">-d</td>
<td align="left">当pathname存在并且是一个目录时返回真</td>
</tr>
<tr>
<td align="left">-e</td>
<td align="left">当pathname指定的文件或目录存在时返回真</td>
</tr>
<tr>
<td align="left">-f</td>
<td align="left">当file存在并且是正规文件时返回真</td>
</tr>
<tr>
<td align="left">-g</td>
<td align="left">当由pathname指定的文件或目录存在并且设置了SGID位时返回为真</td>
</tr>
<tr>
<td align="left">-h</td>
<td align="left">当file存在并且是符号链接文件时返回真，该选项在一些老系统上无效</td>
</tr>
<tr>
<td align="left">-k</td>
<td align="left">当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真</td>
</tr>
<tr>
<td align="left">-p</td>
<td align="left">当file存在并且是命令管道时返回为真</td>
</tr>
<tr>
<td align="left">-r</td>
<td align="left">当由pathname指定的文件或目录存在并且可读时返回为真</td>
</tr>
<tr>
<td align="left">-s</td>
<td align="left">当file存在文件大小大于0时返回真</td>
</tr>
<tr>
<td align="left">-u</td>
<td align="left">当由pathname指定的文件或目录存在并且设置了SUID位时返回真</td>
</tr>
<tr>
<td align="left">-w</td>
<td align="left">当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。</td>
</tr>
<tr>
<td align="left">-o</td>
<td align="left">当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。</td>
</tr>
</tbody></table>
<h1 id="字符串、数字比较"><a href="#字符串、数字比较" class="headerlink" title="字符串、数字比较"></a>字符串、数字比较</h1><table>
<thead>
<tr>
<th align="left">运算符</th>
<th align="left">描述</th>
<th align="left">示例</th>
</tr>
</thead>
<tbody><tr>
<td align="left">-e filename</td>
<td align="left">如果 filename 存在，则为真</td>
<td align="left">[ -e /var/log/syslog ]</td>
</tr>
<tr>
<td align="left">-d filename</td>
<td align="left">如果 filename 为目录，则为真</td>
<td align="left">[ -d /tmp/mydir ]</td>
</tr>
<tr>
<td align="left">-f filename</td>
<td align="left">如果 filename 为常规文件，则为真</td>
<td align="left">[ -f /usr/bin/grep ]</td>
</tr>
<tr>
<td align="left">-L filename</td>
<td align="left">如果 filename 为符号链接，则为真</td>
<td align="left">[ -L /usr/bin/grep ]</td>
</tr>
<tr>
<td align="left">-r filename</td>
<td align="left">如果 filename 可读，则为真</td>
<td align="left">[ -r /var/log/syslog ]</td>
</tr>
<tr>
<td align="left">-w filename</td>
<td align="left">如果 filename 可写，则为真</td>
<td align="left">[ -w /var/mytmp.txt ]</td>
</tr>
<tr>
<td align="left">-x filename</td>
<td align="left">如果 filename 可执行，则为真</td>
<td align="left">[ -L /usr/bin/grep ]</td>
</tr>
<tr>
<td align="left">-z string</td>
<td align="left">如果 string 长度为零，则为真</td>
<td align="left">[ -z $myvar ]</td>
</tr>
<tr>
<td align="left">-n string</td>
<td align="left">如果 string 长度非零，则为真</td>
<td align="left">[ -n $myvar ]</td>
</tr>
<tr>
<td align="left">string1 = string2</td>
<td align="left">如果 string1 与 string2 相同，则为真</td>
<td align="left">[ $myvar = one two three ]</td>
</tr>
<tr>
<td align="left">string1 != string2</td>
<td align="left">如果 string1 与 string2 不同，则为真</td>
<td align="left">[ $myvar != one two three ]</td>
</tr>
<tr>
<td align="left">num1 -eq num2</td>
<td align="left">等于</td>
<td align="left">[ 3 -eq $mynum ]</td>
</tr>
<tr>
<td align="left">num1 -ne num2</td>
<td align="left">不等于</td>
<td align="left">[ 3 -ne $mynum ]</td>
</tr>
<tr>
<td align="left">num1 -lt num2</td>
<td align="left">小于</td>
<td align="left">[ 3 -lt $mynum ]</td>
</tr>
<tr>
<td align="left">num1 -le num2</td>
<td align="left">小于或等于</td>
<td align="left">[ 3 -le $mynum ]</td>
</tr>
<tr>
<td align="left">num1 -gt num2</td>
<td align="left">大于</td>
<td align="left">[ 3 -gt $mynum ]</td>
</tr>
<tr>
<td align="left">num1 -ge num2</td>
<td align="left">大于或等于</td>
<td align="left">[ 3 -ge $mynum ]</td>
</tr>
</tbody></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">　　filename1 -nt filename2 如果 filename1 比 filename2 新，则为真 [ /tmp/install/etc/services -nt /etc/services ]</span><br><span class="line">　　filename1 -ot filename2   如果 filename1 比 filename2 旧，则为真  [ /boot/bzImage -ot arch/i386/boot/bzImage ]</span><br></pre></td></tr></table></figure>

<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li>[1] <a target="_blank" rel="noopener" href="https://www.cnblogs.com/image-eye/archive/2011/08/20/2147015.html">Linux中使用的if语句</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/11/12/2020/2020-11-12-bash/" data-id="ckrn3an71000l20xw3yr69kvr" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/11/16/2020/2020-11-16-supervisor/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Supervisor使用详解
        
      </div>
    </a>
  
  
    <a href="/2020/11/10/2020/2020-11-10-linux-env-init/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Linux-环境配置</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/3D/" rel="tag">3D</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Game/" rel="tag">Game</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Go/" rel="tag">Go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Math/" rel="tag">Math</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Server/" rel="tag">Server</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Unreal-Engine4/" rel="tag">Unreal Engine4</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/golang/" rel="tag">golang</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/guitar/" rel="tag">guitar</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node-js/" rel="tag">node.js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/" rel="tag">protobuf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/recast/" rel="tag">recast</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/type/" rel="tag">type</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/" rel="tag">vscode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag">运维</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/3D/" style="font-size: 10px;">3D</a> <a href="/tags/AI/" style="font-size: 11.67px;">AI</a> <a href="/tags/C/" style="font-size: 15px;">C++</a> <a href="/tags/Game/" style="font-size: 18.33px;">Game</a> <a href="/tags/Go/" style="font-size: 13.33px;">Go</a> <a href="/tags/Linux/" style="font-size: 13.33px;">Linux</a> <a href="/tags/Math/" style="font-size: 11.67px;">Math</a> <a href="/tags/Server/" style="font-size: 16.67px;">Server</a> <a href="/tags/Unreal-Engine4/" style="font-size: 10px;">Unreal Engine4</a> <a href="/tags/git/" style="font-size: 10px;">git</a> <a href="/tags/golang/" style="font-size: 20px;">golang</a> <a href="/tags/guitar/" style="font-size: 10px;">guitar</a> <a href="/tags/node-js/" style="font-size: 10px;">node.js</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/python/" style="font-size: 10px;">python</a> <a href="/tags/recast/" style="font-size: 11.67px;">recast</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/type/" style="font-size: 10px;">type</a> <a href="/tags/vscode/" style="font-size: 10px;">vscode</a> <a href="/tags/%E8%BF%90%E7%BB%B4/" style="font-size: 16.67px;">运维</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">April 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">November 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">November 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/19/2021/2021-07-19-etcd-learn/">go-etcd阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/19/2021/2021-07-19-socketop-learn/">REUSEPORT-REUSEADDR-学习</a>
          </li>
        
          <li>
            <a href="/2021/06/22/2021/2021-06-22-go-ethereum-learn/">go-ethereum阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/06/21/2021/2021-06-21-server%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/">server测试工具</a>
          </li>
        
          <li>
            <a href="/2021/06/09/2021/2021-06-09-pprof-doc/">go-pprof</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 Abel Sean<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>